package com.xwks.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xwks.common.Result;
import com.xwks.entity.General;
import com.xwks.entity.GeneralDetail;
import com.xwks.entity.GeneralSkill;
import com.xwks.entity.GeneralSkin;
import com.xwks.service.GeneralDetailService;
import com.xwks.service.GeneralService;
import com.xwks.service.GeneralSkillService;
import com.xwks.service.GeneralSkinService;
import com.xwks.vo.GeneralDetailVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.Objects;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xwks
 * @since 2023-09-09
 */
@RestController
@RequestMapping("/general")
@Api(tags = "武将信息")
@Slf4j
public class GeneralController {

    @Autowired
    private GeneralService generalService;

    @Autowired
    private GeneralDetailService generalDetailService;

    @Autowired
    private GeneralSkillService generalSkillService;

    @Autowired
    private GeneralSkinService generalSkinService;

    @PostMapping("/generalList")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", value = "页码", type = "Long"),
            @ApiImplicitParam(name = "size", value = "页面大小", type = "Long"),
    })
    @ApiOperation("分页查询列表")
    public Result<General> generalList(Long current,Long size) {

        try {
            if (Objects.isNull(current)) {
                current = 1L;
            }
            if (Objects.isNull(size)) {
                size = 10L;
            }
            IPage<General> page = new Page<>(current, size);
            return Result.success(generalService.page(page));
        } catch (Exception e) {
            log.error(e.getMessage());
            return Result.error();
        }
    }

    @ApiOperation("武将详情")
    @PostMapping("/generalDetail")
    public Result<GeneralDetailVo> generalDetail(Integer id) {

        if (Objects.isNull(id)) {
            return Result.error();
        }

        try {
            GeneralDetailVo vo = new GeneralDetailVo();
            vo.setGeneralDetail(generalDetailService.getOne(new QueryWrapper<GeneralDetail>().eq("general_id", id).eq("del_flag", 0)));
            vo.setSkillList(generalSkillService.list(new QueryWrapper<GeneralSkill>().eq("general_id", id).eq("del_flag", 0)));
            vo.setSkinList(generalSkinService.list(new QueryWrapper<GeneralSkin>().eq("general_id", id).eq("del_flag", 0)));
            return Result.success(vo);
        } catch (Exception e) {
            log.error(e.getMessage());
            return Result.error();
        }
    }

}
